home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 240 (DVD) / Issue 240 - February 2008 - DPCS0208DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / Style_Template_English / selectorsView / selectors.js < prev   
Encoding:
JavaScript  |  2007-04-11  |  13.7 KB  |  562 lines

  1. var fmPreview;
  2. var fmSelectors;
  3. var lstSelectors;
  4. var CSSurl          = '../../preview.css';
  5. var selPreview;
  6. var selPreviewWait  = false;
  7. var previewLoaded   = false;
  8. var curTemplatePath;
  9. var curSelObject;
  10. var templates       = './template/';
  11. var defaultTemplate = 'default.html';
  12. var textTemplate    = 'text.html';
  13. var formTemplate    = 'form.html';
  14. var tsTemplate      = 'tableStyle.html';
  15. var defaultClass    = '.nof_defaultPreview';
  16. var implicitRules   = ['BODY','.nof_tag_body',
  17.                        'A','.nof_text_link',
  18.                        'A:visited','.nof_text_visited',
  19.                        'A:active','.nof_text_active',
  20.                        'A:hover','.nof_text_hover'
  21.                       ];
  22.  
  23. function init() {
  24.   fmPreview     = document.frames['preview'];
  25.   fmSelectors   = document.frames['selectors'];
  26. }
  27.  
  28. function initSelectors(doc) {
  29.   var innerInitSelectors = function() {
  30.     fmSelectors.document.onmousedown = callMouseDown;
  31.     lstSelectors = fmSelectors.document.getElementById('selectors');
  32.     if (lstSelectors.length != 0) {
  33.       lstSelectors.value = lstSelectors[0].value;
  34.       lstSelectors.focus();
  35.       lstSelectors.fireEvent('onchange');
  36.     }
  37.     else {
  38.       setPreview('BODY')
  39.     }
  40.   }
  41.  
  42.   if (fmSelectors) {
  43.     innerInitSelectors();
  44.   }
  45.   else  {
  46.     var intvInnerInitSelectors = window.setInterval(function() {
  47.       if (fmSelectors) {
  48.         innerInitSelectors();
  49.         window.clearInterval(intvInnerInitSelectors);
  50.       }
  51.     }, 50);
  52.   }  
  53. }
  54.  
  55. function initPreview(doc) {
  56.   var innerInitPreview = function() {
  57.     previewLoaded = true;
  58.     fmPreview.document.onmousedown = callMouseDown;
  59.     fmPreview.document.onmousemove = callMouseMove;
  60.     selPreview = fmPreview.document.getElementById('selection');
  61.   }
  62.  
  63.   if (fmPreview) {
  64.     innerInitPreview();
  65.   }
  66.   else  {
  67.     var intvInnerInitPreview = window.setInterval(function() {
  68.       if (fmPreview) {
  69.         innerInitPreview();
  70.         window.clearInterval(intvInnerInitPreview);
  71.       }
  72.     }, 50);
  73.   }  
  74. }
  75.  
  76. function setSelPreviewByValue(val) {
  77.   var obj = getPreviewObj(val);
  78.   if (obj) {
  79.     curSelObject = obj.sourceIndex;
  80.     fmPreview.window.scrollTo(0, getPos(obj).top - fmSelectors.document.body.offsetHeight/2);
  81.   }
  82.   else {
  83.     curSelObject = null;
  84.   }
  85.   setSelPreview(obj);
  86. }
  87.  
  88. function refreshSelPreview() {
  89.   var innerRefreshSelPreview = function() {
  90.     if (curSelObject) {
  91.       setSelPreview(fmPreview.document.all[curSelObject]);    
  92.     }
  93.     else {
  94.       var val = lstSelectors ? lstSelectors.value : null;
  95.       if (val) {
  96.         setSelPreview(getPreviewObj(val));
  97.       }
  98.     }
  99.   }
  100.  
  101.   if (previewLoaded) {
  102.     innerRefreshSelPreview();
  103.   }
  104.   else  {
  105.     var intvInnerRefreshSelPreview = window.setInterval(function() {
  106.       if (previewLoaded) {
  107.         innerRefreshSelPreview();
  108.         window.clearInterval(intvInnerRefreshSelPreview);
  109.       }
  110.     }, 50);
  111.   }
  112. }
  113.  
  114. function setPreview(val) {
  115.   NOF_StyleView_CallInspector(val);
  116.   
  117.   var template = defaultTemplate;
  118.  
  119.   if (isText(val))      template = textTemplate;
  120.   else if (isForm(val)) template = formTemplate;
  121.   else if (isTS(val))   template = tsTemplate;
  122.   else if (isNOF(val))  template = val.split('_',2)[1]+'.html';
  123.  
  124.   var path = templates+template;
  125.   if (curTemplatePath != path) {
  126.     previewLoaded = false;
  127.     fmPreview.location.href = curTemplatePath = path;
  128.   }
  129.   
  130.   if (template == defaultTemplate) {
  131.     if (previewLoaded) {
  132.       copyRule(val,defaultClass);
  133.     }
  134.     else  {
  135.       var intvCopyRule = window.setInterval(function() {
  136.         if (previewLoaded) {
  137.           copyRule(val,defaultClass);
  138.           window.clearInterval(intvCopyRule);
  139.         }      
  140.       }, 50);
  141.     }
  142.   }
  143.   
  144.   if (template == textTemplate) {
  145.     if (previewLoaded) {
  146.       copyImplicitRules();
  147.     }
  148.     else  {
  149.       var intvCopyImplicitRules = window.setInterval(function() {
  150.         if (previewLoaded) {
  151.           copyImplicitRules();
  152.           window.clearInterval(intvCopyImplicitRules);
  153.         }
  154.       }, 50);
  155.     }
  156.   }
  157.   
  158.   if (template == tsTemplate && curTemplatePath == path) {
  159.     fmPreview.location.href = path;
  160.   }
  161.   
  162.   if (previewLoaded) {
  163.     setSelPreviewByValue(val);
  164.   }
  165.   else  {
  166.     var intvSetSelPreviewByValue = window.setInterval(function() {
  167.       if (previewLoaded) {
  168.         setSelPreviewByValue(val);
  169.         window.clearInterval(intvSetSelPreviewByValue);
  170.       }
  171.     }, 50);
  172.   }
  173. }
  174.  
  175. function setSelector(obj) {
  176.   if (!selPreviewWait) {
  177.     selPreviewWait = true;
  178.  
  179.     if (obj) {
  180.       curSelObject = obj.sourceIndex;
  181.       var val = getFirstTag(obj, 'input').value;
  182.  
  183.       if (lstSelectors.value != val) {
  184.         NOF_StyleView_CallInspector(val);
  185.         var opt = getFirstOption(lstSelectors, val);
  186.         opt ? opt.selected = true : lstSelectors.value = '';
  187.       }
  188.     }
  189.     else {
  190.       NOF_StyleView_CallInspector(val);
  191.     }
  192.     setSelPreview(obj);
  193.   }
  194.   window.setTimeout("selPreviewWait=false", 500);
  195. }
  196.  
  197. function copyRule(sSrc,sDest) {
  198.   var cssSheet = fmPreview.document.styleSheets[0];
  199.   var oDest = getRule(sSrc);
  200.  
  201.   if (oDest && oDest.style.cssText.length != 0) {
  202.     getStyleSheet(CSSurl).addRule(sDest,oDest.style.cssText);
  203.   }
  204. }
  205.  
  206. function getStyleSheet(val) {
  207.   var cssSheet = fmPreview.document.styleSheets[0];
  208.   var i=0;
  209.   do {
  210.     if (cssSheet.imports[i].href == val) {
  211.       return cssSheet.imports[i];
  212.     }
  213.     i++
  214.   }
  215.   while (i<cssSheet.imports.length && cssSheet.imports[i-1].href != CSSurl);
  216.   return null;
  217. }
  218.  
  219. function getRule(val) {
  220.   var cssSheet = fmPreview.document.styleSheets[0];
  221.   var i=0;
  222.   do {
  223.     if (cssSheet.imports[i].href == CSSurl) {
  224.       var j=0;
  225.       var cssPreview = cssSheet.imports[i];
  226.       do {
  227.         //if (isEqualCI(cssPreview.rules[j].selectorText, val) && cssPreview.rules[j].style.cssText.indexOf(':') != -1) {
  228.         if (cssPreview.rules[j] && isEqualCI(cssPreview.rules[j].selectorText, val)) {
  229.           return cssPreview.rules[j];
  230.         }
  231.         j++
  232.       }
  233.       while (j<cssPreview.rules.length && isNotEqualCI(cssPreview.rules[j-1].selectorText, val));
  234.     }
  235.     i++
  236.   }
  237.   while (i<cssSheet.imports.length && cssSheet.imports[i-1].href != CSSurl);
  238.   return null;
  239. }
  240.  
  241. function refreshCSS(sURL) {
  242.   fmPreview.document.documentElement.style.cursor = 'wait';
  243.   var cssSheet = fmPreview.document.styleSheets[0];
  244.   var i=0;
  245.   do {
  246.     if (cssSheet.imports[i].href == sURL) {
  247.       cssSheet.removeImport(i);
  248.       cssSheet.addImport(sURL,i);
  249.       
  250.       if (curTemplatePath == templates+textTemplate) {
  251.         copyImplicitRules();
  252.       }
  253.       else if (curTemplatePath == templates+defaultTemplate) {
  254.         copyRule(lstSelectors.value,defaultClass);
  255.       }
  256.       else if (curTemplatePath == templates+tsTemplate) {
  257.         fmPreview.location.reload(true);
  258.       }
  259.       else {
  260.         //TODO: Should not have to reload, addImport does not seem to find the last rule added!
  261.         var x = fmPreview.document.documentElement.scrollTop;
  262.         var y = fmPreview.document.documentElement.scrollLeft;
  263.  
  264.         previewLoaded = false;
  265.         fmPreview.location.reload(true);
  266.         
  267.         var innerScroll = function() {
  268.           fmPreview.scrollTo(y, x);
  269.         }
  270.  
  271.         if (previewLoaded) {
  272.           innerScroll();
  273.         }
  274.         else  {
  275.           var intvInnerScroll = window.setInterval(function() {
  276.             if (previewLoaded) {
  277.               innerScroll();
  278.               window.clearInterval(intvInnerScroll);
  279.             }
  280.           }, 50);
  281.         }
  282.       }
  283.       
  284.       refreshSelPreview();
  285.     }
  286.     i++
  287.   }
  288.   while (i<cssSheet.imports.length && cssSheet.imports[i-1].href != sURL);
  289.   fmPreview.document.documentElement.style.cursor = 'auto';
  290. }
  291.  
  292. function copyImplicitRules() {
  293.   for (var i=0; i<implicitRules.length; i++) {
  294.     copyRule(implicitRules[i],implicitRules[i+1]);
  295.     i++
  296.   }
  297. }
  298.  
  299. function setSelPreview(obj) {
  300.   if (obj) {
  301.     var pos = getPos(obj);
  302.     selPreview.style.top     = pos.top + 'px';
  303.     selPreview.style.left    = pos.left + 'px';
  304.  
  305.     selPreview.style.width   = obj.offsetWidth;
  306.     selPreview.style.height  = obj.offsetHeight;
  307.  
  308.     selPreview.style.display = 'block';
  309.   }
  310.   else {
  311.     if (selPreview) selPreview.style.display = 'none';
  312.     //lstSelectors.value = null;
  313.   }
  314. }
  315.  
  316. function appendSelector(val) {
  317.   //TODO:  getRule falsely returns null on new items from Forms temaplte
  318.   //if (isNotEqualCI(lstSelectors.value, val) && getRule(val) != null) {
  319.   if (isNotEqualCI(lstSelectors.value, val)) {
  320.     var optn = fmSelectors.document.createElement("OPTION");
  321.     optn.text = val;
  322.     optn.value = val;
  323.     lstSelectors.options.add(optn);
  324.     lstSelectors.value = val;
  325.     refreshSelPreview();
  326.   }  
  327. }
  328.  
  329. function getPreviewObj(val) {
  330.   var elms = fmPreview.document.all.tags('INPUT');
  331.   if (elms) {
  332.     for (var i=0; i<elms.length; i++) {
  333.       if (isEqualCI(elms[i].value, val)) {
  334.         return elms[i].parentElement;
  335.       }
  336.     }
  337.   }
  338.   return null;
  339. }
  340.  
  341. function getQueryString(doc) {
  342.   var qs = new Object();
  343.   var query = doc.location.search.substring(1).split('&');
  344.   for (var i in query) {
  345.     var nv = query[i].split('=');
  346.     qs[nv[0]] = nv[1];
  347.   }
  348.   return qs;
  349. }
  350.  
  351. function getCSSImport(path) {
  352.   var s  = '\n';
  353.       s += '<style type="text/css">\n';
  354.       s += '  @import url("' +path+ '");\n';
  355.       s += '</style>\n';
  356.   return s;
  357. }
  358.  
  359. function getPos(obj) {
  360.   var curLeft = 0;
  361.   var curTop = 0;
  362.   if (obj.offsetParent) {
  363.     curLeft = obj.offsetLeft;
  364.     curTop = obj.offsetTop;
  365.     while (obj = obj.offsetParent) {
  366.       curLeft += obj.offsetLeft;
  367.       curTop += obj.offsetTop;
  368.     }
  369.   }
  370.   return { left:curLeft, top:curTop };
  371. }
  372.  
  373. function getFirstOption(list, val) {
  374.   for (var i=0; i<list.length; i++) {
  375.     if (isEqualCI(list.options[i].value, val)) {
  376.       return list.options[i];
  377.     }
  378.   }
  379.   return null;
  380. }
  381.  
  382. function getFirstTag(container, tag) {
  383.   for (var i=0; i<container.all.length; i++) {
  384.     if (isEqualCI(container.all[i].tagName, tag)) {
  385.       return container.all[i];
  386.     }
  387.   }
  388.   return null;
  389. }
  390.  
  391. function callMouseMove() {
  392.   if (fmPreview.event) {
  393.     if (fmPreview.event.clientX < 3) {
  394.       fmPreview.document.documentElement.style.cursor = 'col-resize';
  395.     }
  396.     else {
  397.       fmPreview.document.documentElement.style.cursor = 'auto';
  398.     }
  399.   }
  400. }
  401.  
  402. function isImplicitRule(val) {
  403.  for (var i=0; i<implicitRules.length; i++) {
  404.   if (implicitRules[i] = val) {
  405.     return i;
  406.   }
  407.   i++; 
  408.  }
  409.  return -1;
  410. }
  411.  
  412. function isNOF(val) {
  413.   var valid = false;
  414.   if (val.indexOf('.nof_') != -1) {
  415.     var path = '';
  416.     
  417.     if (navigator.userAgent.indexOf("MSIE 6") != -1) {
  418.       var aPathName = unescape(window.location.pathname).split('\\');
  419.  
  420.       for (var i=0; i<aPathName.length-1; i++)  {
  421.         path += (i != 0) ? aPathName[i] : aPathName[i].substr(1);
  422.         path += '/';
  423.       }
  424.     }
  425.     else {
  426.       var aPathName = unescape(window.location.pathname).split('/');
  427.  
  428.       for (var i=1; i<aPathName.length-1; i++)  {
  429.         path += aPathName[i];
  430.         path += '/';
  431.       }
  432.     }
  433.  
  434.     path += templates + val.split('_',2)[1]+'.html';
  435.     valid = app.DoesFileExist(path);
  436.   }  
  437.   
  438.   return valid;
  439. }
  440.  
  441. function isTS(val) {
  442.   return (val.indexOf('.nof_') != -1 && val.indexOf('TableStyles') != -1);
  443. }
  444.  
  445. function isForm(val) {
  446.   var elements = [
  447.     'form',
  448.     'input, select, textarea',
  449.     /*
  450.     '.nof_form_input_text',
  451.     '.nof_form_input_password',
  452.     '.nof_form_input_checkbox',
  453.     '.nof_form_input_radio',
  454.     '.nof_form_input_submit',
  455.     '.nof_form_input_image',
  456.     '.nof_form_input_reset',
  457.     '.nof_form_input_button',
  458.     '.nof_form_input_file',
  459.     */
  460.     'select',
  461.     'select optgroup',
  462.     'select option',
  463.     '.nof_form_listBox',
  464.     /*
  465.     '.nof_form_listBox_optgroup',
  466.     '.nof_form_listBox_option',
  467.     */
  468.     'textarea',
  469.     'label',
  470.     'fieldset',
  471.     'legend',
  472.     
  473.     // Hover Pseudo-class
  474.     'input:hover, select:hover, textarea:hover',
  475.     /*
  476.     '.nof_form_input_text:hover',
  477.     '.nof_form_input_password:hover',
  478.     '.nof_form_input_checkbox:hover',
  479.     '.nof_form_input_radio:hover',
  480.     '.nof_form_input_submit:hover',
  481.     '.nof_form_input_image:hover',
  482.     '.nof_form_input_reset:hover',
  483.     '.nof_form_input_button:hover',
  484.     '.nof_form_input_file:hover',
  485.     */
  486.     'select:hover',
  487.     'select optgroup:hover',
  488.     'select option:hover',
  489.     /*
  490.     '.nof_form_listBox:hover',
  491.     '.nof_form_listBox_optgroup:hover',
  492.     '.nof_form_listBox_option:hover',
  493.     */
  494.     'textarea:hover',
  495.     'label:hover'
  496.     ];
  497.   
  498.   for (var i=0; i<elements.length; i++) {
  499.     if (isEqualCI(val, elements[i])) {
  500.       return true;
  501.     }
  502.   }
  503.   return false;
  504. }
  505.  
  506. function isText(val) {
  507.   var csElements = [
  508.     //'BODY',
  509.     ];
  510.     
  511.   var ciElements = [
  512.     'body',
  513.     '.textobject',
  514.     'p',
  515.     'a',
  516.     'a:link',
  517.     'a:active',
  518.     'a:visited',
  519.     'a:hover',
  520.     '.textnavbar',
  521.     'h1',
  522.     'h2',
  523.     'h3',
  524.     'h4',
  525.     'h5',
  526.     'h6',
  527.     'pre',
  528.     'blockquote',
  529.     'address',
  530.     'ol',
  531.     'ol li',
  532.     'ul',
  533.     'ul li',
  534.     'ul li li',
  535.     'ul li li li'
  536.     ];
  537.   
  538.   var boo = false;
  539.   
  540.   for (var i=0; i<csElements.length; i++) {
  541.     if (val == csElements[i]) {
  542.       boo = true;
  543.     }
  544.   }
  545.   for (var i=0; i<ciElements.length; i++) {
  546.     if (isEqualCI(val, ciElements[i])) {
  547.       boo = true;
  548.     }
  549.   }
  550.  
  551.   return boo;
  552. }
  553.  
  554. function isEqualCI(arg1, arg2) {               
  555.   var boo = arg1.toLowerCase() == arg2.toLowerCase()
  556.   return boo;
  557. }
  558.  
  559. function isNotEqualCI(arg1, arg2) {               
  560.   var boo = arg1.toLowerCase() != arg2.toLowerCase()
  561.   return boo;
  562. }